package com.hf.eagle.web.struts.admin.manager;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.hf.eagle.domain.NewsInfo;
import com.hf.eagle.domain.OrderDealInfo;
import com.hf.eagle.domain.OrderInfo;
import com.hf.eagle.domain.OrderProductDetail;
import com.hf.eagle.domain.SysDictionary;
import com.hf.eagle.domain.UserInfo;
import com.hf.eagle.web.base.BaseAction;
import com.hf.eagle.web.base.Keys;
import com.hf.eagle.web.base.Pager;
import com.hf.eagle.web.base.SysConstant;

/**
 * @author Xu,XiaoYuan
 * @version 2011.11.14 14:41
 */
public class OnlineOrderAction extends BaseAction {

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		return this.list(mapping, form, request, response);
	}

	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;
		Pager pager = (Pager) dynaBean.get("pager");
		String name_like = (String) dynaBean.get("name_like");
		String mod_id = (String) dynaBean.get("mod_id");

		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setState(0);
		super.copyProperties(orderInfo, form);
		orderInfo.setMod_id(null);
		orderInfo.getMap().put("name_like", name_like);

		Long recordCount = super.getFacade().getOrderInfoService().getOrderInfoCount(orderInfo);
		pager.init(recordCount, pager.getPageSize(), pager.getRequestPage());
		orderInfo.getRow().setFirst(pager.getFirstRow());
		orderInfo.getRow().setCount(pager.getLastRow());

		List<OrderInfo> list = super.getFacade().getOrderInfoService().getOrderInfoPaginatedList(orderInfo);
		request.setAttribute("entityList", list);

		super.copyProperties(form, orderInfo);
		dynaBean.set("_create_time", orderInfo.getCreate_time());

		NewsInfo promotion = new NewsInfo();
		promotion.setMod_id(2902l);
		promotion.setIs_del(0);
		promotion.getMap().put("efficient_promotion", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
		List<NewsInfo> promotionList = super.getFacade().getNewsInfoService().getNewsInfoList(promotion);
		request.setAttribute("promotionList", promotionList);

		SysDictionary sd = new SysDictionary();
		sd.setDictionary_id(SysConstant.ORDER_INFO_DEAL_MODE);
		sd.setIs_del(0);
		List<SysDictionary> dealmodeList = super.getFacade().getSysDictionaryService().getSysDictionaryList(sd);
		request.setAttribute("dealmodeList", dealmodeList);

		dynaBean.set("mod_id", mod_id);

		return mapping.findForward("list");
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;
		String id = (String) dynaBean.get("id");

		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setId(Long.valueOf(id));
		orderInfo = super.getFacade().getOrderInfoService().getOrderInfo(orderInfo);
		if (null != orderInfo) {
			if (orderInfo.getState() == 99) { // 已归档
				String msg = super.getMessage(request, "orderinfo.archived");
				super.renderJavaScript(response, "alert('" + msg + "');");
				return null;
			}

			orderInfo.setQueryString(super.serialize(request, "mod_id", "id", "method"));

			dynaBean.set("_create_time", orderInfo.getCreate_time());

			if (orderInfo.getMod_id().intValue() != 2902) {
				OrderProductDetail productDetail = new OrderProductDetail();
				productDetail.setOrder_id(orderInfo.getId());
				List<OrderProductDetail> productDetailList = super.getFacade().getOrderProductDetailService()
						.getOrderProductDetailList(productDetail);
				orderInfo.setProductDetailList(productDetailList);
			}

		}
		super.copyProperties(form, orderInfo);

		SysDictionary sd = new SysDictionary();
		sd.setDictionary_id(SysConstant.ORDER_INFO_DEAL_MODE);
		sd.setIs_del(0);
		List<SysDictionary> dealmodeList = super.getFacade().getSysDictionaryService().getSysDictionaryList(sd);
		request.setAttribute("dealmodeList", dealmodeList);

		OrderDealInfo orderDealInfo = new OrderDealInfo();
		orderDealInfo.setOrder_id(Long.valueOf(id));
		List<OrderDealInfo> dealInfolList = getFacade().getOrderDealInfoService().getOrderDealInfoList(orderDealInfo);
		request.setAttribute("dealInfolList", dealInfolList);

		return mapping.findForward("input");
	}

	public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		setNaviStringToScope(request);

		DynaBean dynaBean = (DynaBean) form;
		String id = (String) dynaBean.get("id");

		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setId(Long.valueOf(id));
		orderInfo = super.getFacade().getOrderInfoService().getOrderInfo(orderInfo);
		if (null != orderInfo && orderInfo.getMod_id().intValue() != 2902) {
			OrderProductDetail productDetail = new OrderProductDetail();
			productDetail.setOrder_id(orderInfo.getId());
			List<OrderProductDetail> productDetailList = super.getFacade().getOrderProductDetailService()
					.getOrderProductDetailList(productDetail);
			orderInfo.setProductDetailList(productDetailList);
		}

		super.copyProperties(form, orderInfo);

		OrderDealInfo orderDealInfo = new OrderDealInfo();
		orderDealInfo.setOrder_id(Long.valueOf(id));
		List<OrderDealInfo> dealInfolList = getFacade().getOrderDealInfoService().getOrderDealInfoList(orderDealInfo);
		request.setAttribute("dealInfolList", dealInfolList);

		return mapping.findForward("view");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String mod_id = (String) dynaBean.get("mod_id");
		String id = (String) dynaBean.get("id");
		String deal_mode = (String) dynaBean.get("deal_mode");
		String deal_mode_memo = (String) dynaBean.get("deal_mode_memo");
		String memo = (String) dynaBean.get("memo");
		String queryString = (String) dynaBean.get("queryString");

		HttpSession session = request.getSession();
		UserInfo userInfo = (UserInfo) session.getAttribute(Keys.ADMIN_USER);

		OrderDealInfo orderDealInfo = new OrderDealInfo();
		orderDealInfo.setOrder_id(Long.valueOf(id));
		orderDealInfo.setDeal_user_id(userInfo.getUser_id());
		orderDealInfo.setDeal_user_name(userInfo.getUser_name());
		orderDealInfo.setDeal_mode(Integer.valueOf(deal_mode));
		orderDealInfo.setDeal_mode_memo(deal_mode_memo);
		orderDealInfo.setMemo(memo);

		super.getFacade().getOrderDealInfoService().createOrderDealInfo(orderDealInfo);
		saveMessage(request, "entity.dealed");

		// the line below is added for pagination
		StringBuffer pathBuffer = new StringBuffer();
		pathBuffer.append(mapping.findForward("success").getPath());
		pathBuffer.append("&mod_id=" + mod_id);
		pathBuffer.append("&");
		pathBuffer.append(super.encodeSerializedQueryString(queryString));
		ActionForward forward = new ActionForward(pathBuffer.toString(), true);
		// end
		return forward;
	}

	public ActionForward archive(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DynaBean dynaBean = (DynaBean) form;
		String mod_id = (String) dynaBean.get("mod_id");
		String id = (String) dynaBean.get("id");

		HttpSession session = request.getSession();
		UserInfo userInfo = (UserInfo) session.getAttribute(Keys.ADMIN_USER);

		// 订单状态置为99
		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setId(Long.valueOf(id));
		orderInfo.setState(99);// 归档
		orderInfo.getMap().put("archive", "true");

		// 记录处理记录
		OrderDealInfo orderDealInfo = new OrderDealInfo();
		orderDealInfo.setOrder_id(Long.valueOf(id));
		orderDealInfo.setDeal_user_id(userInfo.getUser_id());
		orderDealInfo.setDeal_user_name(userInfo.getUser_name());
		orderDealInfo.setDeal_mode(99);

		orderInfo.getMap().put("orderDealInfo", orderDealInfo);

		super.getFacade().getOrderInfoService().modifyOrderInfo(orderInfo);
		saveMessage(request, "entity.archived");

		// the line below is added for pagination
		StringBuffer pathBuffer = new StringBuffer();
		pathBuffer.append(mapping.findForward("success").getPath());
		pathBuffer.append("&mod_id=" + mod_id);
		pathBuffer.append("&");
		pathBuffer.append(super.encodeSerializedQueryString(super.serialize(request, "id", "method", "mod_id")));
		ActionForward forward = new ActionForward(pathBuffer.toString(), true);
		// end
		return forward;
	}
}
